package com.lc.qianrenshiye.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lc.qianrenshiye.BaseActivity;
import com.lc.qianrenshiye.BaseApplication;
import com.lc.qianrenshiye.R;
import com.lc.qianrenshiye.conn.Conn;
import com.lc.qianrenshiye.conn.LoginPost;
import com.lc.qianrenshiye.uitls.MyToast;
import com.lc.qianrenshiye.uitls.SetTagUtils;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMD5;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * Created by Administrator on 2016/11/15.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.login_username_et) private TextView login_username_et;
    @BoundView(R.id.login_password_et) private TextView login_password_et;
    @BoundView(R.id.login_forget_password_tv) private TextView login_forget_password_tv;
    @BoundView(R.id.login_submit_tv) private TextView login_submit_tv;
    @BoundView(R.id.login_apply_join_tv) private TextView login_apply_join_tv;
    @BoundView(R.id.login_service_phone_tv) private TextView login_service_phone_tv;

    private LoginPost loginPost = new LoginPost(new AsyCallBack<LoginPost.UserInfo>(){

        @Override
        public void onSuccess(String toast, int type, LoginPost.UserInfo userInfo) throws Exception {

            MyToast.showToast(toast, context);

            startActivity(new Intent(LoginActivity.this, HomeActivity.class));

            BaseApplication.BasePreferences.saveIsLogin(true);
            BaseApplication.BasePreferences.saveUserId(userInfo.uid);
            BaseApplication.BasePreferences.saveShopId(userInfo.sid);

            //设置别名
            SetTagUtils setTagUtils = new SetTagUtils();

            setTagUtils.setTags(context);

            finish();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);

        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        setLogo();

        setTitleName(getString(R.string.login_title_text));

        login_forget_password_tv.setOnClickListener(this);
        login_submit_tv.setOnClickListener(this);
        login_apply_join_tv.setOnClickListener(this);
        login_service_phone_tv.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.login_submit_tv:

                String useName = login_username_et.getText().toString();

                if (TextUtils.isEmpty(useName)) {

                    MyToast.showToast(getString(R.string.please_fill_usename_text_hint), this);
                    return;

                } else if (TextUtils.isEmpty(getPassWord())) {

                    MyToast.showToast(getString(R.string.please_fill_password_hint), this);
                    return;

                }

                loginPost.username = useName;
                loginPost.password = UtilMD5.MD5Encode(UtilMD5.MD5Encode(getPassWord(), "UTF-8"), "UTF-8");
                loginPost.execute(this);

                break;

            case R.id.login_forget_password_tv:

                startActivity(new Intent(this, ChangePasswordActivity.class));

                break;

            case R.id.login_apply_join_tv:

                Bundle bundle = new Bundle();

                bundle.putString("url", Conn.SERVICE + Conn.WEB_LEVEL + "2");
                bundle.putString("title", "申请入驻");

                startActivity(new Intent(this, MyWebActivity.class).putExtras(bundle));

                break;

        }

    }

    private String getPassWord() {

        return login_password_et.getText().toString();
    }




}
